#include "dirStack.h"
#include "main_header.h"


int get_id1(MYSQL *mysql,char *username,char *send_back,char *result,char *id){
    //拼接查询id命令
    char sql_id[1024] = {0};
    sprintf(sql_id,"select id from file where username = '%s' and filename = '%s';",username,result);

    //查询判断该用户是否有此文件，如果文件名和用户名对不上，返回
    int qret = mysql_query(mysql,sql_id);
    if(qret != 0){
        strcpy(send_back,"user does not have this file");
        return -1;
    }
    //取出id的那一行
    MYSQL_RES * res_id  = mysql_store_result(mysql);
    MYSQL_ROW row_id = mysql_fetch_row(res_id);
    if(row_id == NULL){
        strcpy(send_back,"error,no such file");
        mysql_free_result(res_id);
        return -1;
    } 
    //printf("%s=================\n",row_id[0]);
    strcpy(id,row_id[0]);
    mysql_free_result(res_id);
    return 0;
}

int get_id(MYSQL *mysql,char *username,char *send_back,char *result,char *id){
    //拼接查询id命令
    char sql_id[1024] = {0};
    sprintf(sql_id,"select id from file where username = '%s' and path = '%s';",username,result);

    //查询判断该用户是否有此文件，如果文件名和用户名对不上，返回
    int qret = mysql_query(mysql,sql_id);
    if(qret != 0){
        strcpy(send_back,"user does not have this file");
        return -1;
    }
    //取出id的那一行
    MYSQL_RES * res_id  = mysql_store_result(mysql);
    MYSQL_ROW row_id = mysql_fetch_row(res_id);
    if(row_id == NULL){
        strcpy(send_back,"error,no such file");
        mysql_free_result(res_id);
        return -1;
    }
    strcpy(id,row_id[0]);
    mysql_free_result(res_id);

    return 0;
}


int get_path(MYSQL *mysql,char *username,char *send_back,char *result,char *path){
    //拼接查询path命令,取出path那一行
    char sql_path[1024] = {0};
    sprintf(sql_path,"select path from file where username = '%s' and path = '%s';",username,result);
    int qret = mysql_query(mysql,sql_path);
    if(qret != 0){
        strcpy(send_back,"user does not have this path");
        return -1;
    }
    MYSQL_RES *res_path = mysql_store_result(mysql);
    MYSQL_ROW row_path = mysql_fetch_row(res_path);
    if(row_path == NULL){
        strcpy(send_back,"error,no such path");
        mysql_free_result(res_path);
        return -1;
    }
    strcpy(path,row_path[0]);
    mysql_free_result(res_path);
    //printf("%s\n",path);
    return 0;
}



int use_cd(dirStack_t *st, MYSQL *mysql, char *result, char *username, char *send_back) {
    dirStack_t tmp;
    initStack(&tmp);
    
    if(strcmp(result, "")==0){
        initStack(st);
        return -1;
    }
    // 处理绝对/相对路径
    if (result[0] == '/') {
        // 绝对路径从根开始
        char root_id[100] = {0};
        if (get_id(mysql, username, send_back, "/", root_id) != 0) {
            strcpy(send_back, "cannot find root");
            return -1;
        }
        pushStack(&tmp, atoi(root_id));
    } else {
        // 相对路径继承当前栈
        for (int i = 0; i < st->size; ++i) {
            pushStack(&tmp, st->idArr[i]);
        }
    }

    // 处理路径
    char path_copy[1024];
    strcpy(path_copy, result[0] == '/' ? result + 1 : result);
    
    char *token = strtok(path_copy, "/");
    while (token) {
        if (strcmp(token, ".") == 0) {
            // 当前目录，不动
        } else if (strcmp(token, "..") == 0) {
            // 上一级，弹栈
            if (tmp.size > 1) {
                popStack(&tmp);
            }
        } else {
            // 查询当前目录下是否有该目录
            int parent_id = tmp.idArr[tmp.size-1];
            char sql[1024];
            sprintf(sql, "select id from file where username='%s' and parent_id=%d and filename='%s' and is_deleted=0 and type='d'", 
                   username, parent_id, token);
            
            if (mysql_query(mysql, sql) != 0) {
                strcpy(send_back, "query failed");
                return -1;
            }
            
            MYSQL_RES *res = mysql_store_result(mysql);
            MYSQL_ROW row = mysql_fetch_row(res);
            
            if (!row) {
                mysql_free_result(res);
                snprintf(send_back, 1024, "not found: %s", token);
                return -1;
            }
            
            int dir_id = atoi(row[0]);
            mysql_free_result(res);
            pushStack(&tmp, dir_id);
        }
        
        token = strtok(NULL, "/");
    }
    
    // 更新主栈
    initStack(st);
    for (int i = 0; i < tmp.size; ++i) {
        pushStack(st, tmp.idArr[i]);
    }
    
    return 0;
}
int use_pwd(dirStack_t *st,MYSQL *mysql,char *send_back){
    char check_id[1024] = {0};
    if(st->size ==0 || st->idArr[st->size - 1] == -1){
        strcpy(send_back,"/");
        return 0;
    }else{
        int top_id = st->idArr[st->size - 1];
        sprintf(check_id,"select path from file where id = %d",top_id);
        if(mysql_query(mysql,check_id)!=0){
            strcpy(send_back,"select  current_path fail");
            return -1;
        }
        MYSQL_RES *res_pwd = mysql_store_result(mysql);
        MYSQL_ROW row_pwd = mysql_fetch_row(res_pwd);
        if(row_pwd == NULL){
            strcpy(send_back,"get path fail in use_pwd");
            mysql_free_result(res_pwd);
            return -1;
        }
        strcpy(send_back,row_pwd[0]);
    }
    return 0;
}
